package com.starblink.imgsearch.findsimilar.ext

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import com.starblink.android.basic.aroute.CommonRoute
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.basic.ext.load
import com.starblink.imgsearch.databinding.LayoutFindSimilarItemBinding
import com.starblink.rocketreserver.fragment.ProductF

@SuppressLint("StringFormatMatches")
internal fun LayoutFindSimilarItemBinding.setData(product: ProductF?, picUrl: String? = null) {
    val binding = this
    binding.img.enableCorner(12.dp2px())
    binding.img.borderRadius = 12.dp2px().toFloat()
    if (product?.mainImg?.isNotEmpty() == true || picUrl?.isNotEmpty() == true) {
        binding.img.load(product?.mainImg ?: picUrl)
    } else {
        binding.img.setImageDrawable(null)
    }
    //binding.img.setImageResource(com.starblink.android.basic.R.drawable.error_img)
    binding.tvBrandTag.visibleOrGone(product?.brand?.isNotEmpty() == true) {
        text = product!!.brand
    }
    binding.tvName.text = product?.title
//        binding.tvVoteNum.visibleOrGone((product?.voteNum ?: 0) > 0) {
//            text = context.getString(R.string.on_people_wish, "${product!!.voteNum!!}")
//        }

    val propertiesStr = StringBuilder()
    product?.frontPropertyList?.mapNotNull { it.value }?.let {
        it.forEachIndexed { index, s ->
            propertiesStr.append(s)
            if (index < it.size - 1) {
                propertiesStr.append(" | ")
            }
        }
    }
    binding.tvGoodsAtts.visibleOrGone(propertiesStr.isNotEmpty()) {
        text = propertiesStr
    }
    binding.vPrice.visibleOrGone(product?.price != null) {
        show(product?.price ?: 0, product?.currency, product?.currencySymbol)
    }
    binding.tvTag.visibleOrGone(product?.cornerLabels?.isNotEmpty() == true && product.cornerLabels!![0].type.let {
        it == 1 || it == 2 || it == 3
    }) {
        val type = product!!.cornerLabels!![0].type
        backgroundTintList = if (type == 3) {
            ColorStateList.valueOf(color(com.starblink.basic.style.R.color.sk_red600))
        } else {
            ColorStateList.valueOf(color(com.starblink.basic.style.R.color.sk_gray600))
        }
        text = product.cornerLabels!![0].title
    }
    binding.root.setOnClickListener {
        product ?: return@setOnClickListener
        CommonRoute.startProductLink(
            activity = it.context,
            webUrl = product.url,
            id = product.id ?: "", productId = product.productId ?: "",
            standardSpuIds = product.standardSpuIds ?: "",
            collect = product.collected ?: false,
            type = product.type,
            storeId = product.merchantWeb?.id ?: "",
            storeName = product.merchantWeb?.merchantName ?: "",
            storeLogo = product.merchantWeb?.logo,
            storeFollow = product.merchantWeb?.subOrNot ?: false,
            affInfoLink = product.affInfo?.link,
        )
    }
    if (product != null) {
        binding.root.trackData(
            element = SpmElementDef.Element_33057,
            page = SpmPageDef.FIND_SIMILAR_PAGE,
            params = SpmTrackHandler.trackProductMap(0, product),
        )
    }
}